﻿using DDLParser.constant;
using DDLParser.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDLParser
{
    public class DDLTermLookup
    {
        protected List<DDLTerm> termList;
        public bool IsConvertUpperCase { get; set; }

        public DDLTermLookup()
        {
            IsConvertUpperCase = true;
        }

        public void Add(DDLTerm term)
        {
            termList.Add(term);
        }

        public void Sort()
        {
            TermComparator comparator = new TermComparator();
            termList.Sort(comparator);
        }

        public void SetTermList(List<DDLTerm> termList)
        {
            this.termList = termList;
        }

        public int GetType(DDLWord word)
        {
            if (termList == null)
            {
                throw new Exception("Error : null term list.");
            }

            int resultType = DDLTermType.UNKNOWN;
            DDLTerm searchWord = new DDLTerm();
            if (IsConvertUpperCase)
            {
                searchWord.Term = word.Word.ToUpper();
            }
            else
            {
                searchWord.Term = word.Word;
            }

            IComparer<DDLTerm> comparator = new TermComparator();
            int index = termList.BinarySearch(searchWord, comparator);
            if (index >= 0)
            {
                DDLTerm terminater = termList[index];
                resultType = terminater.Type;
            }

            return resultType;
        }
    }
}
